
import {transPoint} from "./utilsHander"
import {drawingStyle} from "./StyleConst"

/**
 * 
 * @param {
 *      centerPoint 初始化中心点<Array>, 
 *      defaultZoom 初始图层级别<Number>,
 *      minZoom 图层最小级别<Number>,
 *      maxZoom 图层最大级别<Number>,
 *      soucre 地图资源<Object>,
 *      soucre 地图渲染容器id<String>
 * } params 
 * @returns 
 */
export function init(params)
{
        let {centerPoint = [110.038365,40.416049],defaultZoom = 5,minZoom = 1,maxZoom = 15,soucre = {},containerId} = params

        bdmapcfg = {...soucre,...bdmapcfg.home}
    
        var map = new BMap.Map(containerId)
    
        map.centerAndZoom(transPoint(centerPoint[0],centerPoint[1]), defaultZoom);  
        map.setMinZoom(minZoom);
        map.setMaxZoom(maxZoom)
        map.enableScrollWheelZoom(); //开启层级缩放

        // 平移缩放控件
        map.addControl(new BMap.NavigationControl());  
        // 默认位于地图右上方 
        map.addControl(new BMap.MapTypeControl({
            mapTypes: [BMAP_NORMAL_MAP,BMAP_SATELLITE_MAP,BMAP_HYBRID_MAP],
            anchor: BMAP_ANCHOR_TOP_RIGHT,
            offset: new BMap.Size(40, 20)
        }))
        
        map.getMaxZoom = ()=>{
            return maxZoom
        }

        return map
}

/**
 * 
 * @param {*} map 百度地图对象 
 * @param {*} callBack 绘画回调
 * @returns 
 */
export function drawingInit(map,callBack,style = drawingStyle){

        let drawing = new BMapLib.DrawingManager(map,{
            isOpen:false,
            enableDrawingTool:false,
            enableCalculate:true,

            circleOptions:style,
            polylineOptions:style,
            polygonOptions:style,
            rectangleOptions:style
        })
        
        drawing.addEventListener('overlaycomplete',(e)=> {
            console.log("结果",e);

            callBack(e)
        })
    
        return drawing;
}

/**
 * @param {*} map
 * 点位自动聚合功能 (未完成) 
 */
export function myMarkerCluster(map){
    map.addEventListener("zoomend", function(e){
        var ZoomNum = map.getMaxZoom();
        console.log("图层",ZoomNum);

        let {markers,zoom} = baidu.cover
        
        if(markers.length > 1,ZoomNum > 10 )
        {
            for (let index = 0; index < markers.length; index++) 
            {
                let marker1 = markers[index].point
                let marker2 = markers[index+1].point

                let juli = baidu.map.getDistance(transPoint(marker1.lon,marker1.lat),transPoint(marker2.lon,marker2.lat)).toFixed(2)
                console.log("距离"+juli+"米");
                index++
            }
        }
        
        let markerClusterer = new BMapLib.MarkerClusterer(map, { markers });

        console.log("聚合",markerClusterer);
        baidu.zoom = ZoomNum
    });
}
